import {Api} from "../../../utils/api";
import http from "../../../utils/http";
import Dialog from "@vant/weapp/dialog/dialog";

Page({
    data: {
        customer: {
            name: '',
            phone: '',
            address: ''
        },
        formItemList: [
            {formType: 'upload', label: '客户图片', prop: 'image'},
            {formType: 'input', label: '客户名称', prop: 'name', required: true, placeholder: '请输入客户名称'},
            {formType: 'input', label: '联系人', prop: 'linkUser', placeholder: '请输入联系人'},
            {formType: 'input', label: '联系电话', prop: 'linkPhone', placeholder: '请输入联系电话'},
            {formType: 'input', label: '联系地址', prop: 'linkAddress', placeholder: '请输入联系地址'},
        ],
        imageList: []
    },
    onLoad: function (options) {

    },
    afterRead(event) {
        const e = event.detail
        const { file } = e.detail;
        const token = wx.getStorageSync('token');
        const _this = this
        wx.uploadFile({
            url: Api.http + Api.uploadImage,
            filePath: file.url,
            name: 'file',
            formData: { type: 'customer' , token},
            success(res) {
                const image = JSON.parse(res.data).data
                _this.setData({ 'customer.image': image, imageList: [{url: image}] });
            },
        });
    },
    deleteImage() {
        this.setData({ 'customer.image': '', imageList: [] });
    },
    changeField(event) {
        const e = event.detail
        const field = e.currentTarget.dataset.field
        const value = e.detail
        this.setData({[`customer.${field}`]: value})
    },
    saveSupplier() {
        const supplier = this.data.supplier
        if (supplier.name === '') {
            Dialog.alert({message: '请输入客户名称'})
            return
        }
        http.postRequest(Api.saveSupplier, supplier, (res) => {
            Dialog.alert({title: '提示', message: '保存成功'}).then(() => {
                wx.navigateBack()
            })
        })
    }
});
